#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe
#
# Distributed under terms of the MIT license.


from fabric.api import run, env, parallel
from fabric.context_managers import cd
from fabric.operations import reboot, put
if not env.hosts:
    env.hosts  = []
    env.hosts += ['120.25.67.133', '120.24.42.18', '120.24.42.5', '112.74.81.246', '120.25.206.79', '112.74.77.6', '112.74.27.58', '112.74.31.67']

def check():
    with cd('/root/ESR_TAOBAO'):
        run("docker ps -a")


def stop_worker():
    with cd('/root/ESR_TAOBAO'):
       try:
           run('docker rm -f worker')
           run('rm -rf /run/worker.pid')
       except:
           pass

def update_node():
    with cd('/root/ESR_TAOBAO'):
        run('git pull')
        run('make init')
        stop_worker()

def init():
    with cd('/root/ESR_TAOBAO'):
        put('/root/ESR_TAOBAO/deploy/local.env', '/root/ESR_TAOBAO/deploy/local.env')
        put('/root/ESR_TAOBAO/deploy/worker', '/etc/monit/conf.d/worker')
        run('echo {} > "/var/lib/monit/id"'.format(env.host))
        run('echo {} > "/etc/hostname"'.format(env.host))
        update_node()
        run('/etc/init.d/monit restart')
